package com.lovo.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.project.bean.BuyBean;
import com.lovo.project.bean.BuyInBean;
import com.lovo.project.bean.BuyInGoodsBean;
import com.lovo.project.bean.WarehouseBean;
import com.lovo.project.mapper.BuyInGoodsMapper;
import com.lovo.project.mapper.BuyInMapper;
import com.lovo.project.mapper.BuyMapper;
import com.lovo.project.mapper.WarehouseMapper;
import com.lovo.project.service.BuyInService;
import com.lovo.project.service.BuyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/buy")
public class BuyController {

    @Autowired
    BuyService buyService;
    @Autowired
    BuyMapper buyMapper;
    @Autowired
    BuyInMapper buyInMapper;
    @Autowired
    BuyInGoodsMapper buyInGoodsMapper;
    @Autowired
    WarehouseMapper warehouseMapper;

    @RequestMapping("/queryBuyList")
    public List<BuyBean> queryBuyList(Integer userId) {
        QueryWrapper<BuyBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<BuyBean> list = buyService.queryBuyList(queryWrapper);
        return list;
    }

    @RequestMapping("/addBuy")
    public String addBuy(String buyCode, String buyGoodsName, String buyBrand, String buyGoodsType, double buyPrice, Integer buyNumber, Integer userId) {
        BuyBean buyBean = new BuyBean();
        buyBean.setBuyCode(buyCode);
        buyBean.setBuyGoodsName(buyGoodsName);
        buyBean.setBuyBrand(buyBrand);
        buyBean.setBuyGoodsType(buyGoodsType);
        buyBean.setBuyPrice(buyPrice);
        buyBean.setBuyNumber(buyNumber);
        buyBean.setUserId(userId);
        buyService.insertBuy(buyBean);
        return "ok";
    }

    @RequestMapping("/deleteBuy")
    public String deleteBuy(Integer buyId) {
        buyService.deleteBuy(buyId);
        return "ok";
    }

    @RequestMapping("/buyStash")
    public String buyStash(String supplierName, String userName, Integer userId) {
        BuyInBean buyInBean = new BuyInBean();
        buyInBean.setBuyInSupplierName(supplierName);
        buyInBean.setBuyInBuyer(userName);
        buyInBean.setBuyInTime(LocalDate.now());
        buyInBean.setUserId(userId);
        buyInMapper.insert(buyInBean);
        Integer id = buyInBean.getBuyInId();
        QueryWrapper<BuyBean> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("user_id", userId);
        List<BuyBean> list1 = buyMapper.selectList(queryWrapper1);
        QueryWrapper<WarehouseBean> queryWrapper = new QueryWrapper<>();
        WarehouseBean warehouseBean = new WarehouseBean();
        BuyInGoodsBean buyInGoodsBean = new BuyInGoodsBean();
        for (BuyBean b : list1) {
            queryWrapper.eq("goods_id", b.getBuyId());
            if (warehouseMapper.selectOne(queryWrapper) != null) {
                warehouseBean.setGoodsBuyPrice(b.getBuyPrice());
                warehouseBean.setGoodsNumber(b.getBuyNumber());
                warehouseMapper.updateById(warehouseBean);
            } else {
                warehouseBean.setGoodsId(b.getBuyId());
                warehouseBean.setGoodsCode(b.getBuyCode());
                warehouseBean.setGoodsName(b.getBuyGoodsName());
                warehouseBean.setGoodsBrand(b.getBuyBrand());
                warehouseBean.setGoodsType(b.getBuyGoodsType());
                warehouseBean.setGoodsNumber(b.getBuyNumber());
                warehouseBean.setGoodsBuyPrice(b.getBuyPrice());
                warehouseBean.setGoodsSalePrice(b.getBuyPrice() + 30);
                warehouseMapper.insert(warehouseBean);
            }
            buyInGoodsBean.setBuyInGoodsBrand(b.getBuyBrand());
            buyInGoodsBean.setBuyInGoodsName(b.getBuyGoodsName());
            buyInGoodsBean.setBuyInGoodsNumber(b.getBuyNumber());
            buyInGoodsBean.setBuyInGoodsPrice(b.getBuyPrice());
            buyInGoodsBean.setBuyInId(id);
            buyInGoodsMapper.insert(buyInGoodsBean);
        }
        QueryWrapper<BuyBean> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("user_id", userId);
        buyMapper.delete(queryWrapper2);
        return "ok";
    }
}
